package com.xqboss.web.controller.synthesis;

import cn.hutool.core.bean.BeanUtil;
import com.xqboss.apps.domain.synthesis.SynthesisBoxRule;
import com.xqboss.apps.dto.IdDto;
import com.xqboss.apps.dto.synthesis.*;
import com.xqboss.apps.service.synthesis.SynthesisBoxRuleService;
import com.xqboss.apps.service.synthesis.SynthesisService;
import com.xqboss.apps.vo.synthesis.SynthesisBoxRuleVo;
import com.xqboss.apps.vo.synthesis.SynthesisPageVo;
import com.xqboss.apps.vo.synthesis.WebSynthesisDetailVo;
import com.xqboss.common.annotation.RepeatSubmit;
import com.xqboss.common.core.controller.BaseController;
import com.xqboss.common.core.domain.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * <p>
 * 合成活动
 * </p>
 *
 * @author 木鱼
 * @since 2023-11-17
 */
@RestController
@Slf4j
@Api(tags = "合成活动")
@RequestMapping("/web/synthesis")
@RequiredArgsConstructor
public class WebSynthesisController extends BaseController {
    private final SynthesisService basicService;

    private final SynthesisBoxRuleService boxRuleService;

    /**
     * 合成活动 列表查询
     */
    @PreAuthorize("@ss.hasPermi('web:synthesis:list')")
    @GetMapping("/list")
    @ApiOperation("列表")
    public R<List<SynthesisPageVo>> loadList(@Validated SynthesisPageDto request) {
        return R.ok(basicService.selectWebPage(request));
    }

    /**
     * 合成活动 根据id查询活动信息
     */
    @PreAuthorize("@ss.hasPermi('web:synthesis:selectBasic')")
    @GetMapping("/selectBasic")
    @ApiOperation("详情")
    public R<WebSynthesisDetailVo> selectBasic(@RequestParam("id")
                                               @ApiParam(value = "活动ID", required = true)
                                               @NotNull(message = "请输入活动ID")
                                               Long id) {
        return R.ok(basicService.selectBasic(id));
    }

    /**
     * 合成活动 修改活动信息
     */
    @PreAuthorize("@ss.hasPermi('web:synthesis:updateBasic')")
    @RepeatSubmit(interval = 1000, justUrl = true)
    @PostMapping("/updateBasic")
    @ApiOperation("活动-编辑")
    public R<Boolean> updateBasic(@Validated @RequestBody SynthesisMainSaveUpdateDto record) {
        return R.ok(basicService.updateBasic(record));
    }

    /**
     * 合成活动 新增活动信息
     */
    @PreAuthorize("@ss.hasPermi('web:synthesis:insertBasic')")
    @RepeatSubmit(interval = 1000, justUrl = true)
    @PostMapping("/insertBasic")
    @ApiOperation("新增")
    public R<Boolean> insertBasic(@Validated @RequestBody SynthesisMainSaveUpdateDto record) {
        return R.ok(basicService.saveBasic(record));
    }

    /**
     * 合成活动-方案绑定-修改方案绑定
     */
    @PreAuthorize("@ss.hasPermi('web:synthesis:update-config')")
    @RepeatSubmit(interval = 1000, justUrl = true)
    @PostMapping("/update-config")
    @ApiOperation("库存-编辑")
    public R<Boolean> updateConfig(@Validated @RequestBody WebSynthesisEditDto params) throws Exception {
        return R.ok(basicService.updateConfig(params));
    }

    @PreAuthorize("@ss.hasPermi('web:synthesis:del_hold_condition')")
    @ApiOperation("删除-配置")
    @PostMapping("/del_hold_condition")
    public R<Boolean> delHoldCondition(@Validated @RequestBody WebSynthesisEditDto params) {
        return R.ok(basicService.delHoldCondition(params));
    }

    @PreAuthorize("@ss.hasPermi('web:synthesis:update-config')")
    @PostMapping("/updateWhiteOnly")
    @ApiOperation("活动-修改白名单库存外显开关")
    public R<Boolean> updateWhiteOnly(@Validated @RequestBody WebSynthesisWhiteOnlyEditDto param) {
        return R.ok(basicService.updateWhiteOnly(param.getId(), param.getWhiteOnly()));
    }



    @PreAuthorize("@ss.hasPermi('web:synthesis:selectBasic')")
    @GetMapping("/box/rule/list")
    @ApiOperation("活动-宝盒规则列表")
    public R<List<SynthesisBoxRuleVo>> boxRuleList(@RequestParam Long synthesisId) {
        return R.ok(BeanUtil.copyToList(boxRuleService.lambdaQuery().eq(SynthesisBoxRule::getSynthesisId, synthesisId).list(), SynthesisBoxRuleVo.class));
    }

    @PreAuthorize("@ss.hasPermi('web:synthesis:selectBasic')")
    @GetMapping("/box/rule/detail")
    @ApiOperation("活动-宝盒规则详情")
    public R<SynthesisBoxRuleVo> boxRuleDetail(@RequestParam Long synthesisBoxRuleId) {
        return R.ok(BeanUtil.copyProperties(boxRuleService.getById(synthesisBoxRuleId), SynthesisBoxRuleVo.class));
    }

    @PreAuthorize("@ss.hasPermi('web:synthesis:update-config')")
    @PostMapping("/box/rule/saveOrUpdate")
    @ApiOperation("活动-宝盒规则保存")
    public R<Boolean> boxRuleSaveOrUpdate(@Validated @RequestBody SynthesisBoxRuleSaveDto param) {
        return R.ok(boxRuleService.saveOrUpdateV2(param));
    }

    @PreAuthorize("@ss.hasPermi('web:synthesis:update-config')")
    @PostMapping("/box/rule/delete")
    @ApiOperation("活动-宝盒规则删除")
    public R<Boolean> boxRuleDelete(@Validated @RequestBody IdDto param) {
        return R.ok(boxRuleService.removeById(param.getId()));
    }
}
